package com.itheima.web.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiNiuUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetMealService setMealService;
    /**
     * 套餐图片文件名处理和七牛云端存储
     * @param setMealImg
     * @return
     */
    @RequestMapping("/uploadImage")
    public Result uploadImage(@RequestParam("setMealImg") MultipartFile setMealImg){
        try {
            String filename = setMealImg.getOriginalFilename();
            if (filename != null) {
                int index = filename.lastIndexOf(".");
                String imgFilename
                        = UUID.randomUUID().toString() + filename.substring(index - 1);

                //upload到云上的文件名进行存储 --> redis
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,imgFilename);

                byte[] bytes = setMealImg.getBytes();
                QiNiuUtil.uploadImage2QiNiu(bytes, imgFilename);
                return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,imgFilename);

            }else{
                return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 新增套餐
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal , Integer[] checkgroupIds){

        try {
            setMealService.add(setmeal , checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @PostMapping("/pageHelper")
    public PageResult pageHelper(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = setMealService.findAll(queryPageBean);

        return pageResult;
    }

    @RequestMapping("/updateSetMeal")
    public Result updateSetMeal(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){

        try{
            setMealService.updateSetMeal(setmeal,checkgroupIds);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }

        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}